package com.liushuai.eas.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushuai.eas.common.Result;
import com.liushuai.eas.pojo.Department;
import com.liushuai.eas.service.DepartmentImp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentImp departmentImp;

    @PostMapping
    public Result<Department> add(@RequestBody Department department){
        if (departmentImp.insert(department)!=1)
            return Result.fail();
        return Result.success(department);
    }

    @PutMapping
    public Result<Department> update(@RequestBody Department department){
        if (departmentImp.updateById(department)!=1)
            return Result.fail(department);
        return Result.success(department);
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        Page<Department> departmentPage = departmentImp.selectPage(new Page<Department>(pageNum, pageSize), Wrappers.<Department>lambdaQuery().like(Department::getName, search));
        return Result.success("数据加载成功",departmentPage);
    }

    @DeleteMapping
    public Result<Department> delete(@RequestBody Department department){
        if (departmentImp.deleteById(department)!=1)
            return Result.fail(department);
        return Result.success(department);
    }

    @GetMapping("/selectAll")
    public Result<List<Department>> selectAll(){
        List<Department> departmentList = departmentImp.queryDepartmentList();

        return Result.success(departmentList);
    }

    @GetMapping("/selectOne")
    public Result<Department> selectOne(Integer department){
        Department theDepartment = departmentImp.selectOne(department);

        return Result.success(theDepartment);
    }

    @GetMapping("/departmentCount")
    public Result<?> departmentCount(){
        int total = 0;
        HashMap<String,Object> map = new HashMap<String,Object>();
        total = departmentImp.queryTotal();
        map.put("id",3);
        map.put("name","学院数量");
        map.put("icon","el-icon-s-flag");
        map.put("total",total);
        return Result.success("数据加载成功",map);
    }

}
